/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2015-07-29     Arda.Fu      first implementation
 */
#include <stdint.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <string.h>
#include <board.h>

#include "usb_pwr.h"
#include "hw_config.h"

/* defined the LED1 pin: PB5 */
#define LED1_PIN    57

#define         USB_STACK_SIZE      512
#define         USB_PRIORITY        10
#define         USB_TICKS           5

static rt_thread_t Thread_usb;

void thread_usb_entery(void *parameter);

int main(void)
{
    rt_device_t new_device;
    uint32_t Speed = 1000;


    /*rt_thread_mdelay(2 * 1000);*/     /* only for debug. */

    /* set LED1 pin mode to output */
    rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);

    /*Thread_usb = rt_thread_create("usb_vcom",
            thread_usb_entery, RT_NULL, USB_STACK_SIZE, USB_PRIORITY, USB_TICKS);
            if(Thread_usb != RT_NULL)
                rt_thread_startup(Thread_usb);*/

    /* find new console device */
    /*new_device = rt_device_find(RT_CONSOLE_DEVICE_NAME);
    if (new_device != RT_NULL)
    {
     set new console device
            rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_DMA_RX | RT_DEVICE_FLAG_STREAM);
    }*/

    rt_thread_delay(200);   /* wait vcom device register finshed */

    my_printf("rt_console_set_device! tick=%d\n",rt_tick_get());
    rt_console_set_device("vcom");
    my_printf("finsh_set_device! tick=%d\n",rt_tick_get());
    finsh_set_device("vcom");


    while (1)
    {
        rt_pin_write(LED1_PIN, PIN_LOW);
        /*rt_kprintf("LED on.tick=%d.\n",rt_tick_get());*/
        /*my_printf("LED on! tick=%d",rt_tick_get());*/
        rt_thread_mdelay(Speed);
        rt_pin_write(LED1_PIN, PIN_HIGH);
        /*rt_kprintf("LED off.tick=%d.\n",rt_tick_get());*/
        rt_thread_mdelay(Speed);
    }
}
void thread_usb_entery(void *parameter)
{
    rt_uint8_t usb_rxdata[65];
    rt_device_t dev = RT_NULL;
    rt_size_t size = 0;
    rt_size_t read_size = 64;
    rt_size_t write_size = 64;

    extern __IO uint8_t Receive_Buffer[64];
    extern __IO  uint32_t Receive_length ;
    extern uint32_t packet_sent;

    rt_thread_delay(200);   /* wait vcom device register finshed */
    /*finsh_set_device("vcom");
    rt_console_set_device("vcom");*/


    /*while(1)
    {
        if(dev == RT_NULL)
        {
            dev = rt_device_find("vcom");
            if (dev) {
                    rt_device_open(dev, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
                    rt_device_open(dev, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_TX_NON_BLOCKING);
                    rt_kprintf("open vcom sucess! tick=%d",rt_tick_get());
                } else {
                    rt_kprintf("open vcom fail! tick=%d",rt_tick_get());

                }
        }


        rt_thread_mdelay(15 *1000);

        memset(usb_rxdata,0,65);
        size = rt_device_read(dev, 0, usb_rxdata, read_size);
        rt_kprintf("rt_device_read. size=%d.data=%s. tick=%d.\r\n",size, usb_rxdata, rt_tick_get());
        //rt_kprintf("read_index -- %d\n", usb_rx.read_index);
        //rt_kprintf("save_index -- %d\n", usb_rx.save_index);
        if (size > 0) {
            memset(usb_rxdata,'w',65);
            rt_device_write(dev, 0, usb_rxdata, write_size);
            rt_device_write(dev, 0, usb_rxdata, size);
        }

    }*/
}
